package com.qf.controller.kucun_controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Gnumber;
import com.qf.bean.Goods;
import com.qf.service.kucun_service.GoodsService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.List;

/**
*
 *                                                 ___----~~~~~~~~~~~------___
 *                                   .  .   ~~//``....          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
*/
@Controller
@RequestMapping("/good")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    /*@RequestMapping("/all")
    public String All(Model model,Integer pageNum){
        if (pageNum==null || pageNum==0){
            pageNum=1;
        }
        PageHelper.startPage(pageNum, 3);
        List<Goods> goods = goodsService.QueryAll();
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
        model.addAttribute("page",goodsPageInfo);
        model.addAttribute("goods",goods);
        return "/kucun/good";
    }*/
    @RequestMapping("/detail")
    public String detail(Model model,Integer gid){
        List<Gnumber> gnumbers = goodsService.QueryNumber(gid);
        model.addAttribute("gnumbers",gnumbers);
        return "/kucun/view";
    }
    @RequestMapping("/findtype")
    public String findtype(Model model, Integer pageNum,String goodname, Integer goodtype){
        if (pageNum==null || pageNum==0){
            pageNum=1;
        }
        PageHelper.startPage(pageNum, 3);
        List<Goods> goods = goodsService.QueryType(goodname,goodtype);
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
        model.addAttribute("page",goodsPageInfo);
        model.addAttribute("goods",goods);
        return "/kucun/good";
    }

    @RequestMapping("/daochu")
    public void download(HttpServletResponse response, int[] ids) throws IOException {
        List<Goods> bgmExcelDownloads = goodsService.findByIds(ids);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("获取excel测试表格");
        HSSFRow row = null;
        row = sheet.createRow(0);//创建第一个单元格
        row.setHeight((short) (26.25 * 20));
        row.createCell(0).setCellValue("库存信息列表");//为第一行单元格设值

        /*为标题设计空间
         * firstRow从第1行开始
         * lastRow从第0行结束
         *从第1个单元格开始
         * 从第3个单元格结束
         */
        CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
        sheet.addMergedRegion(rowRegion);

      /*CellRangeAddress columnRegion = new CellRangeAddress(1,4,0,0);
      sheet.addMergedRegion(columnRegion);*/

        row = sheet.createRow(1);
        row.setHeight((short) (22.50 * 20));//设置行高
        row.createCell(0).setCellValue("序号");//为第一个单元格设值
        row.createCell(1).setCellValue("商品编码");//为第二个单元格设值
        row.createCell(2).setCellValue("商品名称");//为第三个单元格设值
        row.createCell(3).setCellValue("商品条码");//为第四个单元格设值
        row.createCell(4).setCellValue("品牌");
        row.createCell(5).setCellValue("规格");
        row.createCell(6).setCellValue("主库库存");
        row.createCell(7).setCellValue("车库库存");
        row.createCell(8).setCellValue("锁定库存");
        //遍历所获取的数据
        for (int i = 0; i < bgmExcelDownloads.size(); i++) {
            row = sheet.createRow(i + 2);
            Goods goods = bgmExcelDownloads.get(i);
            row.createCell(0).setCellValue(goods.getGid());
            row.createCell(1).setCellValue(goods.getCommodity().getCommCode());
            row.createCell(2).setCellValue(goods.getCommodity().getCommName());
            row.createCell(3).setCellValue(goods.getCommodity().getCommBarCode());
            row.createCell(4).setCellValue(goods.getCommodity().getCommBrand().getBname());
            row.createCell(5).setCellValue(goods.getCommodity().getPackge());
            row.createCell(6).setCellValue(goods.getZnumber());
            row.createCell(7).setCellValue(goods.getCnumber());
            row.createCell(8).setCellValue(goods.getSnumber());
        }

        sheet.setDefaultRowHeight((short) (16.5 * 20));
        //列宽自适应
        for (int i = 0; i <= 13; i++) {
            sheet.autoSizeColumn(i);
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename=KuCun.xls");//默认Excel名称
        wb.write(os);
        os.flush();
        os.close();
    }
}
